/**
 * 
 * SHELL CONTROLLER , for section switching...
 * 2009-11-10
 * 
 * 
 * */
package core.section.shell
{
	import com.components.loader.LoaderCallback;
	import com.components.loader.MediaLoader;
	
	import core.ease.EaseFade;
	import core.section.SectionController;
	import core.section.SectionModel;
	import core.section.events.SectionEvents;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;

	public class ShellController extends SectionController
	{
		//START...
		
		//PROPERTY...
		private var _path:String;
		
		private var loadingSection:DisplayObject;
		
		private var currentSection:SectionController;
		
	
		//CONSTRUCTOR...
		public function ShellController(target:DisplayObject)
		{
		
			super(target);
					
		}
		
		//METHODS...
		
		//overide set init variables...
		protected override function setInitVars():void
		{
			
			view = new ShellView(target);
			
			model = new SectionModel;
			
			pagename = "";
			
			pageURL = "";
			
			pageURL = "";
			
		}
		
		//load section...
		private function loadSection():void
		{
			
			var loader:MediaLoader = new MediaLoader(path);
			
			var callback:LoaderCallback = new LoaderCallback([sectionLoadSuccess],[sectionLoadFail],[sectionLoadProgress]);
			
			model.stopAllStream();
			
			(view as ShellView).hideLoadingBar();
			
			model.load(loader,callback);
			
			(view as ShellView).showLoadingBar();
						
		}
		
		//config section controller events...
		private function configSectionEvent(controller:SectionController):void
		{
			
			controller.addEventListener( SectionEvents.ON_EXIT,sectionEaseOutOverHandler,false,0,true);	
			
			controller.addEventListener( SectionEvents.INIT,readyEaseInHandler,false,0,true);
			
			controller.addEventListener(SectionEvents.READY_FOR_EXIT,onReadyToExit,false,0,true);	
						
		}
		
		//section handlers...
		
		//on section exit...
		private function sectionEaseOutOverHandler(evt:SectionEvents):void
		{
			
			var controller:SectionController = evt.currentTarget as SectionController;
			
			(view as ShellView).removeSection(controller.target);
			
			onSectionRemoved(controller);
									
			currentSection = null;
									
			loadSection();
			
		}
		
			
		//on section initializing...
		private function readyEaseInHandler(evt:SectionEvents):void
		{
			
			var section:SectionController = evt.currentTarget as SectionController;				
												
			section.view.easeIn();
			
		}
		
		
		//on section ready to exit...
		private function onReadyToExit(evt:SectionEvents):void
		{
			
			var section:SectionController = evt.currentTarget as SectionController;		
			
			section.view.easeOut();
						
		}		
				
				
		//section load successs...
		private function sectionLoadSuccess(section:DisplayObject):void
		{
			
			(view as ShellView).hideLoadingBar();
			
			(view as ShellView).addSection(section);
			
			var sectionController:SectionController = (section as MovieClip)["controller"] as SectionController;
			
			currentSection = sectionController;
			
			configSectionEvent(sectionController);			
			
			sectionController.init();
			
			onSectionAdded(sectionController);
						
		}
		
		//section load fail...
		private function sectionLoadFail(evt:Event):void
		{
			
			(view as ShellView).hideLoadingBar();
						
			loadFail(evt);
			
		}
		
		
		//section load progress...
		private function sectionLoadProgress(evt:ProgressEvent):void
		{
			
			(view as ShellView).showProgress(evt.bytesLoaded / evt.bytesTotal);
			
		}
		
		// on section added...
		protected function onSectionAdded(section:SectionController):void
		{
		
			// need to be override...	
			
		}
		
		//on section removed...
		protected function onSectionRemoved(section:SectionController):void
		{
			
			// need to be override...
			
		}
		
		
					
		//set section path...
		public function set path(_path:String):void
		{
			
			if(this._path == _path || view.phase != EaseFade.DISPLAY) return;
			
			this._path = _path;
			
			if(currentSection)
			{
			
				if(currentSection.view.phase != EaseFade.EASE_OUT)
				{
					
					currentSection.view.phase = EaseFade.EASE_OUT;
									
				}
				
			}else{
								
				loadSection();
				
			}
																			
		}
		
		//get section path...
		public function get path():String
		{
			
			return _path;
			
		}
						
		
		//END...
	}
}